package cn.didi.feidi.androidorder.assembly;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.activity.ShowDeliverLocationActivity;
import cn.didi.feidi.androidorder.databinding.DialogDeliverInfoBinding;
import cn.didi.feidi.androidorder.model.OrderUser;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * Created by jinxiong on 2017/8/30.
 */
@RuntimePermissions
public class DeliverInfoFragmentDialog extends BaseDialogFragment {


    DialogDeliverInfoBinding binding;
    OrderUser orderUser;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderUser = (OrderUser) getArguments().getSerializable("orderUser");
        if (orderUser == null) {
            Toast.makeText(getActivity(), "获取OrderUser 错误", Toast.LENGTH_SHORT).show();
            return;
        }

        DeliverInfoFragmentDialogPermissionsDispatcher.getPermissionBlankMethodWithCheck(this);
    }

    @NonNull
    @Override
    protected Dialog newDialog() {


        Dialog dialog = new Dialog(getActivity(), R.style.style_dialog);
        binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.dialog_deliver_info,
                (ViewGroup) getActivity().getWindow().getDecorView(), false);
        dialog.setContentView(binding.getRoot());
        setRightPadding(40).setRightPadding(40);
        return dialog;
    }


    @Override
    protected void addListener() {
        binding.deliverInfoPhoneFl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + orderUser.getWaterDeliverPhoneNumber()));
                startActivity(intent);
                dismiss();

            }
        });


        binding.deliverInfoMessageFl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + orderUser.getWaterDeliverPhoneNumber()));
                startActivity(intent);
                dismiss();


            }
        });

        binding.deliverInfoMapFl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), ShowDeliverLocationActivity.class);
                intent.putExtra("orderUser", orderUser);
                startActivity(intent);
                dismiss();

            }
        });

        binding.deliverInfoCloseIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }


    @Override
    protected void initView() {
        binding.setOrderUser(orderUser);
        binding.setFragment(this);
        Glide.with(this)
                .load(orderUser.getOrderWaBuOrUsList().get(0).getPicUrl())
                .placeholder(R.drawable.logo)
                .into(binding.deliverInfoWaterIv);

    }

    public String getOrderIdString() {
        return "订单号 " + orderUser.getOrderUserId();
    }

    public Drawable getDrawableByStatus(int index) {

        if (index == 2) {

            if (orderUser.getStatusSys() >= 3) {
                return ActivityCompat.getDrawable(getActivity(), R.drawable.bg_dot_blue_16);
            } else {
                return ActivityCompat.getDrawable(getActivity(), R.drawable.bg_dot_gray_16);
            }

        } else {

            if (orderUser.getStatusSys() == 4) {
                return ActivityCompat.getDrawable(getActivity(), R.drawable.bg_dot_blue_16);
            } else {
                return ActivityCompat.getDrawable(getActivity(), R.drawable.bg_dot_gray_16);
            }
        }

    }

    public
    @ColorInt
    int getBackgroundByStatus() {


        if (orderUser.getStatusSys() >= 3) {
            return Color.parseColor("#347cf1");
        } else {
            return Color.parseColor("#d8d8d8");
        }
    }

    public String getDeliverInfo() {

        return "配送员: " + orderUser.getWaterDeliverName() + " " + orderUser.getWaterDeliverPhoneNumber();

    }

    @NeedsPermission({Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS})
    public void getPermissionBlankMethod() {
    }

}
